package com.action;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.common.uilt.BaseAction;
import com.google.gson.Gson;
import com.model.Messages;
import com.model.StaffBean;
import com.service.imp.StaffService;
@Controller
@ParentPackage("defaultpackage")
@Namespace("/back")
@Action(value = "staff", results = {
		@Result(name="addstaff",type="redirectAction",location="tree!dept"),
		@Result(name="input",type="redirectAction",location="user!list"),
		@Result(name = "error", location = "/hello.jsp"), })
public class StaffAction extends BaseAction{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7308907969941561247L;
	@Autowired
	private StaffService staffService;
	private String id;
	private StaffBean staff;
	private String dept_id;
	
	/**
	 * 应加上 struts 的表单验证
	 * @return
	 * @throws Exception
	 */
	public String addstaff() throws Exception{
		
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		Messages m=new Messages();
		try {
			System.out.println(id);
			m=this.staffService.addStaff(this.staff,this.dept_id);
		}catch (Exception e) {
			e.printStackTrace();
			m.setInfo(false);
			m.setText(e.getMessage());
		}
		out.print(gson.toJson(m));
		out.flush();
		out.close();
		return NONE;
	}
	public String editstaff() throws Exception{
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		Messages m=new Messages();
		try {
			m=this.staffService.editStaff(this.staff,this.dept_id);
		}catch (Exception e) {
			e.printStackTrace();
			m.setInfo(false);
			m.setText(e.getMessage());
		}
		out.print(gson.toJson(m));
		out.flush();
		out.close();
		return NONE;
	}
	public String getstaffBydept() throws Exception{
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		List<Map<String,Object>> l=null;
		try {
			System.out.println(id);
			l= this.staffService.getstaffBydept(id,currentPage,maxrecord,orderType,sortField);
		}catch (Exception e) {
			e.printStackTrace();
		}
		out.print(gson.toJson(l));
		out.flush();
		out.close();
		return NONE;
	}
	public String getstaffBydeptToname() throws Exception{
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		List<Map<String,Object>> l=null;
		try {
			System.out.println(id);
			l= this.staffService.getstaffBydeptToname(id);
		}catch (Exception e) {
			e.printStackTrace();
		}
		out.print(gson.toJson(l));
		out.flush();
		out.close();
		return NONE;
	}

	public String delstaff() throws Exception{
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		Messages m=new Messages();
		try {
			m= this.staffService.delStaff(id);
		}catch (Exception e) {
			e.printStackTrace();
			m.setInfo(false);
			m.setText(e.getMessage());
		}
		out.print(gson.toJson(m));
		out.flush();
		out.close();
		return NONE;
	}
	public StaffBean getStaff() {
		return staff;
	}
	public void setStaff(StaffBean staff) {
		this.staff = staff;
	}
	public String getDept_id() {
		return dept_id;
	}
	public void setDept_id(String dept_id) {
		this.dept_id = dept_id;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
}
